package com.jd.finance.core.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.BankApplyMapper;
import com.jd.finance.core.mapper.BankMapper;
import com.jd.finance.core.pojo.entity.Bank;
import com.jd.finance.core.pojo.entity.BankApply;
import com.jd.finance.core.service.BankApplyService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 银行申请表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-25
 */
@Service
public class BankApplyServiceImpl extends ServiceImpl<BankApplyMapper, BankApply> implements BankApplyService {

    @Autowired
    private AdminUtilsServiceImpl adminUtilsService;

    @Autowired
    private BankApplyMapper bankApplyMapper;

    @Autowired
    private OpreateLogService opreateLogService;

    @Resource
    private BankMapper bankMapper;

    /**
     * 申请添加合作银行
     * @param uid
     * @param appLybName
     * @param appLybAgreement
     * @param appLyAddress
     * @param request
     * @return
     */
    @Transactional
    @Override
    public BaseResult insertCoopBank(Integer uid, String appLybName, String appLybAgreement, String appLyAddress, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),appLybName,appLybAgreement,appLyAddress)) {
            log.error("申请信息为空!");
            return BaseResult.error().message("申请信息为空!");
        }

        if (!adminUtilsService.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }

        BankApply bankApply=new BankApply();
        bankApply.setBankName(appLybName);
        bankApply.setBankAddress(appLyAddress);
        bankApply.setConpractInfo(appLybAgreement);
        bankApply.setBankApplyStatus("0");

        int count=bankApplyMapper.insert(bankApply);
        if (count < 1) {
            log.error("申请添加合作银行失败!");
            return BaseResult.ok().data("shenqingtijiaohuizhi",0).message("添加失败!");
        }

        boolean add=opreateLogService.add(uid,"添加","添加申请合作银行", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }

        return BaseResult.ok().data("shenqingtijiaohuizhi",1).message("添加成功!");
    }

    /**
     * 修改合作银行审批状态
     * @param uid
     * @param bankAppId
     * @param appLyStatus
     * @param request
     * @return
     */
    @Override
    public BaseResult updateExamApply(Integer uid, Integer bankAppId, String appLyStatus, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),String.valueOf(bankAppId),appLyStatus)) {
            log.error("申请审批状态信息为空!");
            return BaseResult.error().message("申请审批状态信息为空!");
        }

        if (!adminUtilsService.isGM(uid)) {
            log.error("该用户非总经理身份!");
            return BaseResult.error().message("该用户非总经理身份!");
        }

        QueryWrapper<BankApply> bankApplyQueryWrapper = new QueryWrapper<>();
        bankApplyQueryWrapper.eq("bank_app_id",bankAppId);
        BankApply bankApply=new BankApply();
        if (appLyStatus.equals("通过")) {
            bankApply.setBankApplyStatus("1");
        }
        if (appLyStatus.equals("未通过")) {
            bankApply.setBankApplyStatus("2");
        }

        int count=bankApplyMapper.update(bankApply,bankApplyQueryWrapper);
        if (count < 1) {
            log.error("修改状态失败!");
            return BaseResult.ok().data("xiugaizhuangtai",0).message("修改状态失败!");
        }

        boolean add=opreateLogService.add(uid,"修改","修改合作银行审批状态",IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("xiugaizhuangtai",1).message("修改状态成功!");


    }

    /**
     * 查询 合作银行的申请状态
     * @param pageParam
     * @param uid
     * @param request
     * @return
     */
    @Override
    public Page<BankApply> getExamStatus(Page<BankApply> pageParam, Integer uid, HttpServletRequest request) {
        if (!adminUtilsService.isSale(uid)) {
            log.error("该用户非业务员身份!");
        }
        return bankApplyMapper.selectPage(pageParam,null);
    }

    /**
     * 根据合作银行申请id，查看相应申请信息
     * @param uid
     * @param appLyId
     * @return
     */
    @Override
    public BaseResult getBankApplyInfo(Integer uid, Integer appLyId) {
        if (uid==null || appLyId==null) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsService.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }
        QueryWrapper<BankApply> bankApplyQueryWrapper = new QueryWrapper<>();
        bankApplyQueryWrapper.eq("bank_app_id",appLyId);
        BankApply bankApply=bankApplyMapper.selectOne(bankApplyQueryWrapper);
        if (bankApply==null) {
            log.error("未查询到相关信息!");
            return BaseResult.error().message("未查询到相关信息!");
        }
        Map<String,Object> result=new HashMap<>();
        result.put("id",bankApply.getBankAppId());
        result.put("yinhangName",bankApply.getBankName());
        result.put("yinhangdizh",bankApply.getBankAddress());
        result.put("shenqingtime",bankApply.getCreateTime());
        if (bankApply.getBankApplyStatus().equals("1")) {
            result.put("shenqingstate","通过");
        }else if (bankApply.getBankApplyStatus().equals("2")) {
            result.put("shenqingstate","未通过");
        }else {
            log.error("信息错误!");
        }

        return BaseResult.ok().data(result).message("查询成功!");
    }

    /**
     * 添加合作银行
     * @param uid
     * @param appLyId
     * @param request
     * @return
     */
    @Override
    public BaseResult addCoopBank(Integer uid, Integer appLyId, HttpServletRequest request) {
        if (uid==null || appLyId==null) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsService.isSale(uid)) {
            log.error("该用户非业务员身份!");
            return BaseResult.error().message("该用户非业务员身份!");
        }
        BankApply bankApply=bankApplyMapper.selectById(appLyId);
        if (bankApply.getBankApplyStatus().equals("1")) {
            Bank bank=new Bank();
            bank.setBankName(bankApply.getBankName());
            bank.setBankCity(bankApply.getBankCity());
            bank.setBankAddress(bankApply.getBankAddress());

            int count=bankMapper.insert(bank);
            if (count < 1) {
                log.error("添加失败!");
                return BaseResult.ok().data("tianjiahuizhi",0).message("添加失败!");
            }
        }else if (bankApply.getBankApplyStatus().equals("2")) {
            log.error("该银行未通过审核!");
            return BaseResult.error().message("该银行未通过审核!");
        }else {
            log.error("信息错误!");
            return BaseResult.error().message("信息错误!");
        }

        boolean add=opreateLogService.add(uid,"添加","添加合作银行",IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("tianjiahuizhi",1).message("添加成功!");
    }
}
